package com.cxg.fairy.ec.main.personal.order;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.HorizontalListView.HorizontalListAdapter;
import com.cxg.fairy.ui.recycler.HorizontalListView.HorizontalListView;
import com.cxg.fairy.ui.recycler.HorizontalListView.OrderListHorizontalImageData;
import com.cxg.fairy.util.log.LatteLogger;
import com.cxg.fairy.util.toasty.Toasty;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;
import com.jaredrummler.materialspinner.MaterialSpinner;
import com.rengwuxian.materialedittext.MaterialEditText;

import java.math.BigDecimal;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/24.
 */

public class AfterSalesDelegate extends LatteDelegate implements ISuccess{

    private final static String ORDER_NO = "ORDER_NO";

    private int total = 0;

    public void setTotal(int total) {
        this.total = total;
    }

    private BigDecimal mOrderAmount = null;

    private OrderListHorizontalImageData[] mOrderListHorizontalImageData = null;

    public void setOrderAmount(BigDecimal orderAmount) {
        this.mOrderAmount = orderAmount;
    }
    @BindView(R2.id.hlv_after_goods_img_list)
    HorizontalListView hlvGoodsImgList = null;

    @BindView(R2.id.tv_after_sales_goods_count)
    AppCompatTextView tvGoodsCount = null;

    @BindView(R2.id.tv_after_sales_amount)
    AppCompatTextView tvAmount = null;

    @BindView(R2.id.met_after_sales_remark)
    MaterialEditText metRemark = null;

    @BindView(R2.id.ms_after_sales_reason)
    MaterialSpinner msReason = null;

    public void setmOrderListHorizontalImageData(OrderListHorizontalImageData[] mOrderListHorizontalImageData) {
        this.mOrderListHorizontalImageData = mOrderListHorizontalImageData;
    }

    public static AfterSalesDelegate create(String orderNo) {
        final Bundle args = new Bundle();
        args.putString(ORDER_NO, orderNo);
        AfterSalesDelegate delegate = new AfterSalesDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_after_sale;
    }

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    @OnClick(R2.id.btn_after_sales_commit)
    void onClickAfterSalesCommit() {
        if (checkForm()) {
            LatteLogger.e("refundReasonId=",msReason.getSelectedIndex()+"");
            RestClient.builder().url("order/refundApply")
                    .params("uuid", AccountManager.getSignUUID())
                    .params("orderNo", getArguments().getString(ORDER_NO))
                    .params("refundReasonId", msReason.getSelectedIndex()+1)
                    .loader(getContext())
                    .success(this)
                    .build()
                    .post();
        } else {
            Toasty.error(getContext(), "表单数据有误!请检查后重新提交!").show();
        }
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        HorizontalListAdapter adapter = new HorizontalListAdapter(getContext(), mOrderListHorizontalImageData);
        hlvGoodsImgList.setAdapter(adapter);
        tvAmount.setText("¥"+mOrderAmount);
        tvGoodsCount.setText("共" + total + "件 ＞");

        RestClient.builder()
                .url("order/getThreason")
                .params("uuid", AccountManager.getSignUUID())
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        JSONArray arr  = JSON.parseObject(response).getJSONArray("data");
                        ArrayList list = new ArrayList();
                        for (int i = 0; i<arr.size();i++) {
                            list.add(arr.getJSONObject(i).get("name"));
                        }
                       msReason.setItems(list);
                    }
                })
                .build()
                .post();
    }

    private boolean checkForm() {
        String remark = metRemark.getText().toString();
        if (remark.length() < 4 ) {
            return false;
        } else {
            return true;
        }
    }


    @Override
    public void onSuccess(String response) {
        LatteLogger.e("response=",response);
        int code = JSON.parseObject(response).getInteger("code");
        if (code == 1) {
            Toasty.success(getContext(), "退款申请提交成功!").show();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    getSupportDelegate().pop();
                }
            }, 3000);
        } else {
            Toasty.warning(getContext(), "退款申请提交失败!").show();
        }
    }
}
